package com.newtouch.bxzs.database.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.newtouch.bxzs.common.base.response.BaseResponse;
import com.newtouch.bxzs.common.base.response.RespGenerator;
import com.newtouch.bxzs.database.config.MyLog;
import com.newtouch.bxzs.database.model.bo.AppSimpleUserBO;
import com.newtouch.bxzs.database.model.bo.AppUserDetailBO;
import com.newtouch.bxzs.database.model.bo.AppUserListBO;
import com.newtouch.bxzs.database.model.vo.GetAppUserListVO;
import com.newtouch.bxzs.database.model.vo.GetAppUserVO;
import com.newtouch.bxzs.database.model.vo.UpdateUserPositionVO;
import com.newtouch.bxzs.database.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 维护app用户信息:
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/8 15:38</td>
 * <td>tianshangzhu</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author tianshangzhu
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequestMapping("/appUser")
@Api(tags = "维护app用户信息")
@SuppressWarnings("unchecked")
public class AppUserController {
    @Resource
    private UserService userService;
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(AppUserController.class);

    @PostMapping("/getUserList")
    @ApiOperation("获取登录用户所属机构及以下机构的App用户列表")
    @MyLog(systemFunctionName = "维护app用户信息", systemFunctionUrl = "/appUser/getUserList", userOperationType = 4, userOperationSummary = "获取用户列表")
    public BaseResponse<IPage<AppUserListBO>> getUserList4App(@RequestBody GetAppUserListVO getAppUserListVO) {
        logger.info("获取登录用户所属机构及以下机构的App用户列表");
        IPage<AppUserListBO> result = userService.getUserList4App(getAppUserListVO);
        if (result == null || result.getTotal() == 0) {
            RespGenerator.returnOK("没有查询到相应数据");
        }
        return RespGenerator.returnOK(result);
    }

    @GetMapping("/getUserDetail")
    @ApiOperation("根据用户工号展示银行/保险用户详情信息")
    @MyLog(systemFunctionName = "维护app用户信息", systemFunctionUrl = "/appUser/getUserDetail", userOperationType = 4, userOperationSummary = "查询用户详情信息")
    public BaseResponse<AppUserDetailBO> getUserDetail(@RequestParam @ApiParam(value = "用户id", required = true) String userId) {
        logger.info("根据用户工号展示银行/保险用户详情信息");
        return RespGenerator.returnOK(userService.getUserDetail4App(userId));
    }

    @GetMapping("/resetPassword")
    @ApiOperation("重置用户登录密码")
    @MyLog(systemFunctionName = "维护app用户信息", systemFunctionUrl = "/appUser/resetPassword", userOperationType = 2, userOperationSummary = "重置用户密码")
    public BaseResponse<String> resetPassword(@RequestParam @ApiParam(value = "用户id", required = true) String userId) {
        logger.info("重置用户登录密码");
        userService.resetPassword4Pc(userId);
        return RespGenerator.returnOK("重置密码成功");
    }

    @PostMapping("/getSimpleUser")
    @ApiOperation("App端根据机构查询用户")
    @MyLog(systemFunctionName = "维护app用户信息", systemFunctionUrl = "/appUser/getSimpleUser", userOperationType = 4, userOperationSummary = "查询用户列表")
    public BaseResponse<List<AppSimpleUserBO>> getAppSimpleUser(@RequestBody GetAppUserVO getAppUserVO) {
        logger.info("App端根据机构查询用户");
        List<AppSimpleUserBO> result = userService.getAppSimpleUser(getAppUserVO);
        if (result.isEmpty()) {
            RespGenerator.returnOK("没有查询到相应数据");
        }
        return RespGenerator.returnOK(result);
    }

    @PostMapping("/updateUser")
    @ApiOperation("修改用户信息")
    @MyLog(systemFunctionName = "维护app用户信息", systemFunctionUrl = "/appUser/updateUser", userOperationType = 2, userOperationSummary = "修改用户信息")
    public BaseResponse<String> updateUser(@RequestBody UpdateUserPositionVO updateUserPositionVO) {
        userService.updateUserPosition(updateUserPositionVO);
        return RespGenerator.returnOK("操作成功");
    }

}
